package com.imooc.mail.vo;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.imooc.mail.enums.ResponseEnum;
import lombok.Data;
import org.springframework.validation.BindingResult;

/**
 * @Author wjc
 * Date on 2020/6/4 22:39
 */
@Data
//@JsonSerialize(include = )
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public class ResponseVo<T> {
    private Integer status;
    private String msg;
    private T data;

    public ResponseVo(Integer status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    public ResponseVo(Integer status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    public ResponseVo(Integer status, T data) {
        this.status = status;
        this.msg = "success";
        this.data = data;
    }

    public static <T> ResponseVo<T> success(String msg) {
        return new ResponseVo<T>(ResponseEnum.SUCCESS.getCode(), msg);
    }

    public static <T> ResponseVo<T> success() {
        return new ResponseVo<T>(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getDesc());
    }

    public static <T> ResponseVo<T> success(T data) {
        return new ResponseVo<T>(ResponseEnum.SUCCESS.getCode(), data);
    }

    public static <T> ResponseVo<T> error(ResponseEnum responseEnum) {
        return new ResponseVo<T>(responseEnum.getCode(), responseEnum.getDesc());
    }

    public static <T> ResponseVo<T> error(ResponseEnum responseEnum, String message) {
        return new ResponseVo<T>(responseEnum.getCode(), message);
    }

    public static <T> ResponseVo<T> error(ResponseEnum responseEnum, BindingResult result) {
        return new ResponseVo<T>(responseEnum.getCode(),
                result.getFieldError().getField() + " " + result.getFieldError().getDefaultMessage());
    }


}
